package com.example.liaotian.Fragment.DownFragment;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.example.liaotian.Fragment.UpFragment.ActFragment;
import com.example.liaotian.Fragment.UpFragment.HomeUpFragment;
import com.example.liaotian.Fragment.UpFragment.TalkFragment;
import com.example.liaotian.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页主页
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {
    final private List<Fragment> fragments = new ArrayList<>();
    final private ArrayList<String> title = new ArrayList<>();


    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }



    /**
     * 绑定组件
     * 填入数据
     * 加载组件
     */
    private void initView() {




            title.add("首页");
            title.add("社区讨论");
            title.add("活动组织");
            fragments.add(new HomeUpFragment());
            fragments.add(new TalkFragment());
            fragments.add(new ActFragment());



        TabLayout tabLayout = getView().findViewById(R.id.tabLayout);
        ViewPager viewPager = getView().findViewById(R.id.viewPager);
        /**
         * 这里使用了内部类来实现适配器
         */
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });

        /**
         *把pager和tablayout绑定
         */
        tabLayout.setupWithViewPager(viewPager);





    }


    /**
     *
     * 适配器的内部类
     */
    class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * 把每个fragment填入每个pager
         * @param position
         * @return
         */
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        /**
         * pager的长度就是fragment的数量
         * @return
         */
        public int getCount() {
            return fragments.size();
        }


        /**
         * 给tablayout获取标题
         * @param position
         * @return
         */
        public CharSequence getPageTitle(int position) {
            return title.get(position);
        }
    }

}
